<script setup lang="ts">
import { computed, reactive, ref, watch } from 'vue';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { fetchAddStore, fetchEditStore } from '@/service/api';
import { $t } from '@/locales';
// import { enableStatusOptions, userGenderOptions } from '@/constants/business';

defineOptions({
  name: 'UserOperateDrawer'
});

interface Props {
  /** the type of operation */
  operateType: NaiveUI.TableOperateType;
  /** the edit row data */
  rowData?: Api.Food.Store | null;
}

const props = defineProps<Props>();

interface Emits {
  (e: 'submitted'): void;
}

const emit = defineEmits<Emits>();

const visible = defineModel<boolean>('visible', {
  default: false
});

const { formRef, validate, restoreValidation } = useNaiveForm();
const { defaultRequiredRule } = useFormRules();

const title = computed(() => {
  const titles: Record<NaiveUI.TableOperateType, string> = {
    add: '新增菜单',
    edit: '编辑菜单'
  };
  return titles[props.operateType];
});

type Model = Pick<Api.Food.Store, 'name' | 'unit' | 'count' | 'desire' | 'money' | 'leave' | 'message'>;

const model: Model = reactive(createDefaultModel());

function createDefaultModel(): Model {
  return {
    name: '',
    unit: '',
    count: 0,
    desire: 0,
    money: 0.0,
    leave: 0.0,
    message: ''
  };
}

type RuleKey = Extract<keyof Model, 'userName' | 'status'>;

const rules: Record<RuleKey, App.Global.FormRule> = {
  userName: defaultRequiredRule,
  status: defaultRequiredRule
};

function handleUpdateModelWhenEdit() {
  if (props.operateType === 'add') {
    Object.assign(model, createDefaultModel());
    return;
  }

  if (props.operateType === 'edit' && props.rowData) {
    Object.assign(model, props.rowData);
  }
}

function closeDrawer() {
  visible.value = false;
}

async function handleSubmit() {
  await validate();
  // request
  if (props.operateType === 'add') {
    const { error, data } = await fetchAddStore(model);
    if (!error) {
      // Object.assign(model, data);
      // return;
      console.log('success', data);
    }
    console.log('error', error);

    window.$message?.success('新增成功');
  }

  if (props.operateType === 'edit') {
    const { error, data } = await fetchEditStore(model.id, model);
    if (!error) {
      // Object.assign(model, data);
      // return;
      console.log('success', data);
    }
    console.log('error', error);

    window.$message?.success($t('common.updateSuccess'));
  }

  closeDrawer();
  emit('submitted');
}

watch(visible, () => {
  if (visible.value) {
    handleUpdateModelWhenEdit();
    restoreValidation();
  }
});
</script>

<template>
  <NDrawer v-model:show="visible" display-directive="show" :width="360">
    <NDrawerContent :title="title" :native-scrollbar="false" closable>
      <NForm ref="formRef" :model="model" :rules="rules">
        <NFormItem label="原材料" path="name">
          <NInput v-model:value="model.name" placeholder="原材料" />
        </NFormItem>
        <NFormItem label="单位" path="unit">
          <NInput v-model:value="model.unit" />
        </NFormItem>
        <NFormItem label="当前库存" path="count">
          <NInputNumber v-model:value="model.count" clearable placeholder="当前库存" :precision="2" />
        </NFormItem>
        <NFormItem label="预计采购量" path="desire">
          <NInputNumber v-model:value="model.desire" placeholder="预计采购量" clearable :precision="2" />
        </NFormItem>
        <NFormItem label="单价" path="leave">
          <NInputNumber v-model:value="model.leave" placeholder="单价" clearable :precision="2" />
        </NFormItem>
        <!--
 <NFormItem label="进价" path="money">
          <NInputNumber v-model:value="model.money" placeholder="进价" clearable :precision="2" />
        </NFormItem> 
-->
        <NFormItem label="备注" path="message">
          <NInput v-model:value="model.message" placeholder="供应商" />
        </NFormItem>
      </NForm>
      <template #footer>
        <NSpace :size="16">
          <NButton @click="closeDrawer">{{ $t('common.cancel') }}</NButton>
          <NButton type="primary" @click="handleSubmit">{{ $t('common.confirm') }}</NButton>
        </NSpace>
      </template>
    </NDrawerContent>
  </NDrawer>
</template>

<style scoped></style>
